<template>
    <div id="target">
      <div class="newsTitle">
        <div>{{$t('userAdmin.news.title')}}</div>
      </div>
      <el-table
        v-if="noResponse"
        v-loading="loading"
        style="width: 100%;height: 419px;margin: 10px auto;z-index: 1">
      </el-table>
      <el-empty v-if="isEmpty" :image-size="200"></el-empty>
      <div class="myNews-box"v-for="(item,index) in contentData" :key="index">
        <div class="myNews-title-box">
          <div class="myNews-style2" v-if="item.systemNewsType=='警告'">{{item.systemNewsType}}</div>
          <div class="myNews-style1" v-else>{{item.systemNewsType}}</div>
          <div class="myNews-title">
            <span>【{{item.systemNewsTitle}}】</span>
          </div>
          <div class="myNews-time">
            <span>{{item.systemNewsTimingPublishDate | formatDate}}</span>
          </div>
          <diV class="myNews-delete">
            <a href="javascript:;" :title="$t('userAdmin.news.ignoreNews')" @click="onclickIgnoreNews(item.systemNewsId,item.systemNewsType)">
              <i class="iconfont icon-hulve"></i>
            </a>
          </diV>
        </div>
        <div class="myNews-content">
          <div>
            {{item.systemNewsContent}}
          </div>
        </div>
      </div>
      <el-pagination
        v-if="!isEmpty && !noResponse"
        background
        layout="prev, pager, next"
        :total=pageTotal
        @current-change="handleCurrentChange(currentPage)"
        :current-page.sync = currentPage
        class="page">
      </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "UserAdminMyNews",
        data(){
            return{
                loading:"true",
                isEmpty:false,
                noResponse: true,
                baseURL:'http://localhost:8081/userAdmin/getSystemNews/',
                baseURL2:'http://localhost:8081/userAdmin/getPages/systemNewsEarlyNow',
                baseURL3:'http://localhost:8081/userAdmin/ignoreNews/',
                contentData:'',
                pageTotal:0,
                currentPage:1  // 当前页
            }
        },
        mounted() {
            this.$emit("component","myNews")
            this.getNewsInfo()
            this.getPages()
        },
        methods:{
            getNewsInfo(){
                this.axios.post(this.baseURL + this.currentPage).then(res => {
                    this.noResponse = false
                    if (res.data != "empty") {
                        this.contentData = res.data
                    }else {
                        this.isEmpty = true
                        this.contentData = ''
                    }
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },
            getPages(){
                this.axios.post(this.baseURL2).then(res => {
                    this.pageTotal = res.data * 10
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },
            onclickIgnoreNews(newId,type){
                this.$confirm(this.$t('userAdmin.news.confirm.info') + type + '?', {
                    confirmButtonText: this.$t('userAdmin.news.confirm.confirm'),
                    cancelButtonText: this.$t('userAdmin.news.confirm.cancel'),
                    showClose: false,
                    type: 'warning'
                }).then(() => {
                    this.axios.post(this.baseURL3 + newId).then(res=>{
                        if (res.data == true) {
                            this.successOpt(this.$t('userAdmin.news.confirm.successTip'))
                            this.getNewsInfo()
                        } else {
                            this.failOpt(this.$t('userAdmin.news.confirm.faiTip'))
                        }
                    }).catch(
                        exception=>{
                            console.log(exception)
                        }
                    )
                }).catch(() => {
                    console.log(this.$t('userAdmin.news.confirm.cancelTip'))
                });
            },
            handleCurrentChange(currentPage){
                this.axios.post(this.baseURL+currentPage).then(res=>{
                    this.noResponse=false
                    if (res.data != "empty"){
                        this.contentData = res.data
                        let target = document.getElementById("target").offsetTop
                        window.scrollTo({
                            top:target,
                            behavior: "smooth"
                        });
                    }
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },

            successOpt(message) {
                this.$notify({
                    title: this.$t('notify.success'),
                    message: message,
                    type: 'success'
                })
            },
            failOpt(message) {
                this.$notify.error({
                    title: this.$t('notify.error'),
                    message: message
                });
            }
        }
    }
</script>

<style scoped>
  .myNews-box{
    width: 100%;
    min-height: 100px;
    border-radius: 3px;
    letter-spacing: 1.5px;
    margin-bottom: 10px;
    background-color: white;
    transition: 0.2s;
  }

  .myNews-box:hover{
    box-shadow: 0 0 5px rgb(196, 196, 196);
    transform:translateY(-2px);
  }
  .newsTitle{
    width: 100%;
    background-color: white;
    margin-bottom: 15px;
    border-radius: 3px;
  }
  .newsTitle div{
    height: 50px;
    line-height: 50px;
    font-size: 18px;
    color: #414141;
    margin-left: 20px;
    letter-spacing: 1.5px;
  }
  .myNews-title-box{
    width: 100%;
    height: 40px;
    line-height: 40px;
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
  }
  .myNews-title-box div{
    float: left;
  }
  .myNews-style1, .myNews-style2{
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    line-height: 19px;
    text-align: center;
    border-radius: 2px;
    border: 1px solid #9ad88b;
    color: #00a303;
    margin-top: 10px;
    margin-left: 20px;
    background-color: #f0ffe1;
  }
  .myNews-style2{
    border: 1px solid #d89972;
    color: #c60f00;
    background-color: #ffedda;
  }
  .myNews-title{
    width: 580px;
    height: 40px;
    line-height: 40px;
    font-size: 15px;
    padding: 0 10px;
    color: #2b3c3b;
    font-weight: bolder;

    /* 文字溢出处理 */
    display: -webkit-box;
    overflow:hidden;
    text-overflow: ellipsis;
    -webkit-box-orient:vertical;
    -webkit-line-clamp:1;
  }
  .myNews-time{
    width: 200px;
    height: 40px;
    line-height: 45px;
    font-size: 13px;
    color: #8c939d;
    padding: 0 10px;
  }
  .myNews-delete{
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  .myNews-delete i{
    color: #b4b1b5;
    font-size: 19px;
  }
  .myNews-delete i:hover{
    color: #ee1c00;
  }
  .myNews-content{
    width: 100%;
    padding-top: 20px;
    padding-bottom: 20px;
    line-height: 60px;
    border-top: 1px solid #eaeaea;
  }
  .myNews-content div{
    width: 95%;
    min-height: 20px;
    line-height: 25px;
    letter-spacing: 2px;
    padding: 0px 20px;
    font-size: 15px;
    color: #4b4b4b;
  }
  .page{
    margin: 10px auto;
    margin-left: -10px;
  }
</style>
